package com.hzncc.scaffold.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.hzncc.scaffold.entity.Bo.PersonalCenterMyCourseListBo;
import com.hzncc.scaffold.entity.Other.Course;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.hzncc.scaffold.entity.Vo.CourseChaptersDatilVo;
import com.hzncc.scaffold.entity.Vo.CourseDataDetailVo;
import com.hzncc.scaffold.entity.Vo.MyCourseListTwoVo;
import com.hzncc.scaffold.entity.Vo.MyCourseListVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * <p>
 * 课程表 Mapper 接口
 * </p>
 *
 * @author DrunkBear
 * @since 2022-04-27
 */
@Component
public interface CourseMapper extends BaseMapper<Course> {

    @Select("SELECT\n" +
            "\tuser.*,\n" +
            "\t`userjoinedcourse`.`userId` AS `UserJoinedCourseUserId` ,\n" +
            "\t`userjoinedcourse`.`courseId` AS `UserJoinedCourseCourseId`\n" +
            "FROM\n" +
            "\t`user` AS `user`\n" +
            "\tINNER JOIN `userjoinedcourse` AS `userjoinedcourse` ON `user`.`id` = `userjoinedcourse`.`userId` ${ew.customSqlSegment}")
    public List<CourseDataDetailVo> detail(@Param(Constants.WRAPPER) QueryWrapper queryWrapper);


    @Select("SELECT course.title FROM `course`")
    public List<String> getTitle();


    @Select("SELECT\n" +
            "\t`course`.*,\n" +
            "\t`publisher`.`name` AS `publisherName`,\n" +
            "\t`majors`.`id` AS `majorsId`,\n" +
            "\t`majors`.`name` AS `majorsName`,\n" +
            "\t`majors`.`pid` AS `majorsPid` \n" +
            "FROM\n" +
            "\t(\n" +
            "\tSELECT *\n" +
            "\tFROM\n" +
            "\t\t`course` AS `course` \n" +
            "\tORDER BY\n" +
            "\t\t`course`.`id` DESC \n" +
            "\t) AS `course`\n" +
            "\tLEFT OUTER JOIN `user` AS `publisher` ON `course`.`publisherId` = `publisher`.`id`\n" +
            "\tLEFT OUTER JOIN ( `coursemajor` AS `majors->coursemajor` INNER JOIN `major` AS `majors` ON `majors`.`id` = `majors->coursemajor`.`majorId` ) ON `course`.`id` = `majors->coursemajor`.`courseId` \n  ${ew.customSqlSegment}" +
            "ORDER BY\n" +
            "\t`course`.`id` DESC")
    public List<Course> listVo(@Param(Constants.WRAPPER) QueryWrapper queryWrapper);

        @Select("SELECT COUNT(DISTINCT(course.id)) FROM course WHERE course.id IN (SELECT\n" +
                "\t`course`.id\n" +
                "FROM\n" +
                "\t(\n" +
                "\tSELECT\n" +
                "\t\t`course`.`id`,\n" +
                "\t\t`course`.`title`,\n" +
                "\t\t`course`.`status`,\n" +
                "\t\t`course`.`teacher`,\n" +
                "\t\t`course`.`authentation`,\n" +
                "\t\t`course`.`pubTime`,\n" +
                "\t\t`course`.`createTime`,\n" +
                "\t\t`course`.`publisherId`,\n" +
                "\t\t`course`.`isDelete` \n" +
                "\t\t`course`.`isFree` \n" +
                "\tFROM\n" +
                "\t\t`course` AS `course` \n" +
                "\tORDER BY\n" +
                "\t\t`course`.`id` DESC \n" +
                "\t) AS `course`\n" +
                "\tLEFT OUTER JOIN `user` AS `publisher` ON `course`.`publisherId` = `publisher`.`id`\n" +
                "\tLEFT OUTER JOIN ( `coursemajor` AS `majors->coursemajor` INNER JOIN `major` AS `majors` ON `majors`.`id` = `majors->coursemajor`.`majorId` ) ON `course`.`id` = `majors->coursemajor`.`courseId`\n ${ew.customSqlSegment}" +
                "ORDER BY\n" +
                "\t`course`.`id` DESC) ")
    public Integer selectTotal(@Param(Constants.WRAPPER) QueryWrapper queryWrapper);


    @Select("SELECT\n" +
            "\tcourse.id,\n" +
            "\tcourse.title,\n" +
            "\tcourse.cover, \n" +
            "\tcourse.teacher,\n" +
            "\tcourse.isFree\n" +
            "FROM\n" +
            "\t`course`\n" +
            "LEFT JOIN userjoinedcourse ON userjoinedcourse.courseId = course.id ${ew.customSqlSegment}")
    public List<MyCourseListVo> myCourseList(@Param(Constants.WRAPPER) QueryWrapper queryWrapper);

    @Select("SELECT\n" +
            "\tcourse.id,\n" +
            "\tcourse.title,\n" +
            "\tcourse.cover,\n" +
            "\tcourse.teacher,\n" +
            "\tcourse.isFree \n" +
            "FROM\n" +
            "\t`course`\n" +
            "\tLEFT JOIN usercollectcourse ON usercollectcourse.courseId = course.id ${ew.customSqlSegment}")
    public List<MyCourseListTwoVo> myCourseListTwo(@Param(Constants.WRAPPER) QueryWrapper queryWrapper);

    @Select("SELECT\n" +
            "\tcourse.id,\n" +
            "\tcourse.title,\n" +
            "\tcourse.cover, \n" +
            "\tcourse.teacher,\n" +
            "\tcourse.teacherInfo,\n" +
            "\tcourse.courseInfo,\n" +
            "\tcourse.joinedNum,\n" +
            "\tcourse.isFree\n" +
            "FROM\n" +
            "\t`course`\n" +
            "LEFT JOIN userjoinedcourse ON userjoinedcourse.courseId = course.id ${ew.customSqlSegment}")
    public CourseChaptersDatilVo courseDetail(@Param(Constants.WRAPPER) QueryWrapper queryWrapper);

    @Select("SELECT\n" +
            "\tcourse.id,\n" +
            "\tcourse.title,\n" +
            "\tcourse.cover, \n" +
            "\tcourse.teacher,\n" +
            "\tcourse.teacherInfo,\n" +
            "\tcourse.courseInfo,\n" +
            "\tcourse.joinedNum,\n" +
            "\tcourse.isFree\n" +
            "FROM\n" +
            "\t`course`\n ${ew.customSqlSegment}")
    public CourseChaptersDatilVo courseDetailPlus(@Param(Constants.WRAPPER) QueryWrapper queryWrapper);

    @Select("SELECT userId from userjoinedcourse where courseId = #{courseId}")
    public List<Integer> courseUsers(@Param("courseId") Integer courseId);

    @Select("SELECT\n" +
            "\tcourse.id,\n" +
            "\tcourse.title,\n" +
            "\tcourse.cover, \n" +
            "\tcourse.teacher,\n" +
            "\tcourse.isFree\n" +
            "FROM\n" +
            "\t`course`\n" +
            "LEFT JOIN userjoinedcourse ON userjoinedcourse.courseId = course.id ${ew.customSqlSegment}")
    public List<MyCourseListVo> isJoined(@Param(Constants.WRAPPER) QueryWrapper queryWrapper);




    @Select("SELECT\n" +
            "\t`course`.`id`,\n" +
            "\t`course`.`title`,\n" +
            "\t`Learner`.`id` AS `LearnerId` \n" +
            "FROM\n" +
            "\t`course` AS `course`\n" +
            "\tINNER JOIN ( `userjoinedcourse` AS `Learner->userjoinedcourse` INNER JOIN `user` AS `Learner` ON `Learner`.`id` = `Learner->userjoinedcourse`.`userId` ) ON `course`.`id` = `Learner->userjoinedcourse`.`courseId` ${ew.customSqlSegment}")
    public List<PersonalCenterMyCourseListBo> PersonalCentermyCourseList(@Param(Constants.WRAPPER) QueryWrapper queryWrapper);
}
